package com.ruoyi.attend.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.attend.utils.DateDeal;
import com.ruoyi.common.config.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.attend.mapper.DayUserMapper;
import com.ruoyi.attend.domain.DayUser;
import com.ruoyi.attend.service.IDayUserService;
import com.ruoyi.common.support.Convert;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 打卡记录 服务层实现
 *
 * @author wangxx
 * @date 2018-11-23
 */
@Service
public class DayUserServiceImpl implements IDayUserService {
    @Autowired
    private DayUserMapper dayUserMapper;

    /**
     * 查询打卡记录信息
     *
     * @param cardUserId 打卡记录ID
     * @return 打卡记录信息
     */
    @Override
    public DayUser selectDayUserById(Integer cardUserId) {
        return dayUserMapper.selectDayUserById(cardUserId);
    }

    /**
     * 查询打卡记录列表
     *
     * @param dayUser 打卡记录信息
     * @return 打卡记录集合
     */
    @Override
    public List<DayUser> selectDayUserList(DayUser dayUser) {
        return dayUserMapper.selectDayUserList(dayUser);
    }

    /**
     * 新增打卡记录
     *
     * @param dayUser 打卡记录信息
     * @return 结果
     */
    @Override
    public int insertDayUser(DayUser dayUser) {
        return dayUserMapper.insertDayUser(dayUser);
    }

    /**
     * 修改打卡记录
     *
     * @param dayUser 打卡记录信息
     * @return 结果
     */
    @Override
    public int updateDayUser(DayUser dayUser) {
        return dayUserMapper.updateDayUser(dayUser);
    }

    /**
     * 删除打卡记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDayUserByIds(String ids) {
        return dayUserMapper.deleteDayUserByIds(Convert.toStrArray(ids));
    }

    @Value("${ruoyi.profile}")
    private String path;

    @Value("${server.servlet.context-path}")
    private String contxtePath;

    @Override
    public DayUser uploadImage(DayUser dayUser, MultipartFile file) {
        if (file != null) {
            if (file.getName() != null || "".equals(file.getName())) {
                String[] name = file.getContentType().split("/");
                if ("BMP".equals(name[name.length - 1]) || "JPG".equals(name[name.length - 1])
                        || "JPEG".equals(name[name.length - 1]) || "bmp".equals(name[name.length - 1])
                        || "jpg".equals(name[name.length - 1]) || "jpeg".equals(name[name.length - 1])) {
                    // 物理地址
                    try {
                        String[] split = file.getOriginalFilename().split("\\.");
                        String bfix= split[1];
                        String num =DateDeal.getSHC()+"."+bfix;
                       String  fileName= Global.getAvatarPath() + num;//头像上传的路径
                        File file1 = new File(fileName);
                        if (!file1.getParentFile().exists()){
                            file1.getParentFile().mkdirs();
                        }
                        file.transferTo(file1);
                        dayUser.setStatus("");
                        dayUser.setMsg("上传成功!");
                        dayUser.setPathImge(fileName);
                        // 网络地址
                        dayUser.setAvatar("/" + num);
                        // 先删除原来的文件，再将网络地址写入数据库
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return dayUser;
                } else {
					/*messageEntity.setState(1);
					messageEntity.setMsg("格式不正确!");*/
                    dayUser.setStatus("1");
                    dayUser.setMsg("格式不正确!");
                    return dayUser;
                }
            } else {
				/*messageEntity.setState(1);
				messageEntity.setMsg("请选择文件!");*/
                dayUser.setStatus("1");
                dayUser.setMsg("请选择文件!");
                return dayUser;
            }
        } else {
			/*messageEntity.setState(1);
			messageEntity.setMsg("请选择文件!");*/
            dayUser.setStatus("1");
            dayUser.setMsg("请选择文件!");
            return dayUser;
        }
        //return messageEntity;

    }

}
